//
//  CLNovalView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/4/26.
//

#import "CLNovelView.h"
#import "CLVCNovelCatalogue.h"

@interface CLNovelIntroView : CLBaseView
@property (nonatomic, strong) UIImageView *ivLogo;
//@property (nonatomic, strong) UILabel *lbTitle;
@property (nonatomic, strong) UILabel *lbIntro;
@property (nonatomic, strong) CLBook *book;
@end

@interface CLNovelView ()
@property (nonatomic, strong) CLNovelIntroView *introView;
@end

@implementation CLNovelView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configSubViews];
    }
    return self;
}

- (void)configSubViews {
    WEAK_SELF;
    self.introView = [[CLNovelIntroView alloc] initWithFrame:self.bounds];
    [self addSubview:self.introView];
    
    UIButton *btnCatalogue = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:btnCatalogue];
    btnCatalogue.backgroundColor = [UIColor whiteColor];
    [btnCatalogue setAttributedTitle:[[NSAttributedString alloc] initWithString:@"打开目录" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}] forState:UIControlStateNormal];
    [[btnCatalogue rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (!weakSelf.book.catalogue.count) {
            [weakSelf showHUDWithMessage:@"没有目录"];
            return;
        }
        CLVCNovelCatalogue *vc = [[CLVCNovelCatalogue alloc] init];
        vc.book = weakSelf.book;
        vc.delegate = weakSelf.delegate;
        [CLNavigationService presentViewController:vc animated:YES completion:nil];
    }];
    
    UIButton *btnRead = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:btnRead];
    btnRead.backgroundColor = [UIColor whiteColor];
    [btnRead setAttributedTitle:[[NSAttributedString alloc] initWithString:@"立即阅读" attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}] forState:UIControlStateNormal];
    [btnRead addTarget:self action:@selector(readRightNow) forControlEvents:UIControlEventTouchUpInside];
    
    [btnCatalogue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(60);
        make.width.equalTo(self).multipliedBy(0.5);
        make.bottom.left.equalTo(self);
    }];
    
    [btnRead mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(60);
        make.width.equalTo(self).multipliedBy(0.5);
        make.bottom.right.equalTo(self);
    }];
}

- (void)setBook:(CLBook *)book {
    _book = book;
    self.introView.book = book;
//    WEAK_SELF;
    [book addObserverWithUpdateBlock:^{
//        [weakSelf.tableView addModels:weakSelf.book.catalogue];
//        [weakSelf.tableView reloadData];
    }];
}

- (void)readRightNow {
    [self cl_novelViewOpenChapterWithIndex:0];
}

- (void)cl_novelViewOpenChapterWithIndex:(NSUInteger)index {
    WEAK_SELF;
    CLReadChapterModel *model = [self.book.catalogue objectAtIndex:index];
    if ([weakSelf.delegate respondsToSelector:@selector(cl_novelViewOpenChapter:indexPath:)]) {
        [weakSelf.delegate cl_novelViewOpenChapter:model indexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    }
}


@end


@implementation CLNovelIntroView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configSubViews];
    }
    return self;
}

- (void)configSubViews {
    self.backgroundColor = [UIColor whiteColor];
//    [self addSubview:self.lbTitle];
    [self addSubview:self.ivLogo];
    [self addSubview:self.lbIntro];
    
//    [self.lbTitle mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.centerX.equalTo(self);
//        make.top.equalTo(self).offset(80);
//    }];
    
    [self.ivLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(120, 160));
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(90);
    }];
    
    [self.lbIntro mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self).offset(-40);
        make.centerX.equalTo(self);
        make.top.equalTo(self.ivLogo.mas_bottom).offset(10);
    }];
    
}

- (void)setBook:(CLBook *)book {
//    self.lbTitle.text = book.title;
    self.lbIntro.text = book.intro;
    [self.ivLogo sd_setImageWithURL:[NSURL URLWithString:book.logo]];
}


- (UILabel *)lbIntro {
    if (!_lbIntro) {
        _lbIntro = [[UILabel alloc] init];
        _lbIntro.numberOfLines = 0;
        _lbIntro.font = [UIFont systemFontOfSize:12];
    }
    return _lbIntro;
}

//- (UILabel *)lbTitle {
//    if (!_lbTitle) {
//        _lbTitle = [[UILabel alloc] init];
//        _lbTitle.font = [UIFont systemFontOfSize:18];
//        _lbTitle.textAlignment = NSTextAlignmentCenter;
//    }
//    return _lbTitle;
//}

- (UIImageView *)ivLogo {
    if (!_ivLogo) {
        _ivLogo = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 160)];
        _ivLogo.backgroundColor = [UIColor grayColor];
        _ivLogo.layer.cornerRadius = 10;
        _ivLogo.layer.masksToBounds = YES;
    }
    return _ivLogo;
}

@end
